package com.fivefu.core.skyeye.event.services.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fivefu.base.common.utils.date.DateUtils;
import com.fivefu.core.skyeye.event.entity.DbAiSceneType;
import com.fivefu.core.skyeye.event.mapper.DbAiSceneTypeMapper;
import com.fivefu.core.skyeye.event.services.DbAiSceneTypeService;
import com.fivefu.core.skyeye.event.vo.SceneTypeVo;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Service
public class DbAiSceneTypeServiceImpl extends ServiceImpl<DbAiSceneTypeMapper, DbAiSceneType> implements DbAiSceneTypeService {

    /**
     * 分页获取识别类型列表
     * @param page 当前页
     * @param limit 每页显示条数
     * @return
     */
    @Override
    public Page<Map<String, Object>> querySceneTypeList(Integer page, Integer limit) {
        Page<Map<String,Object>> backPage = new Page<>();
        List<Map<String,Object>> backList = new ArrayList<>();

        List<DbAiSceneType> list = new ArrayList<>();
        QueryWrapper<DbAiSceneType> wrapper = new QueryWrapper<>();
        wrapper.eq("is_delete",0);
        if (page != null && limit != null){
            Page<DbAiSceneType> iPage = new Page<>(page,limit);
            iPage = baseMapper.selectPage(iPage,wrapper);
            list = iPage.getRecords();
            backPage.setTotal(iPage.getTotal());
        }else {
            list = baseMapper.selectList(wrapper);
            backPage.setTotal(list.size());
        }

        list.forEach(sceneType -> {
            backList.add(formatSceneType(sceneType));
        });
        backPage.setRecords(backList);

        return backPage;
    }

    /**
     * 通过编号查询识别类型
     * @param id 识别类型编号
     * @return
     */
    @Override
    public Map<String, Object> querySceneTypeDetail(Long id) {
        DbAiSceneType sceneType = baseMapper.selectById(id);
        Map<String, Object> map = formatSceneType(sceneType);

        return map;
    }

    /**
     * 格式化识别类型信息
     * @param sceneType
     * @return
     */
    public Map<String, Object> formatSceneType(DbAiSceneType sceneType){
        Map<String,Object> map = JSON.parseObject(JSON.toJSONString(sceneType, SerializerFeature.WriteMapNullValue), Map.class);
        if (sceneType.getCreatedTime() != null){
            String time = DateUtils.format(sceneType.getCreatedTime(),DateUtils.DEFAULT_DATETIME_PATTERN);
            map.put("createdTime", time);
        }
        if (sceneType.getUpdatedTime() != null){
            String time = DateUtils.format(sceneType.getUpdatedTime(),DateUtils.DEFAULT_DATETIME_PATTERN);
            map.put("updatedTime", time);
        }
        return map;
    }

    /**
     * 编辑识别类型
     * @param sceneTypeVo 识别类型信息
     * @param userid 操作用户编号
     */
    @Override
    public void editSceneType(SceneTypeVo sceneTypeVo, String userid){
        LocalDateTime time = LocalDateTime.now();
        DbAiSceneType sceneType = sceneTypeVo.formatSceneType();
        sceneType.setUpdatedBy(userid);
        sceneType.setUpdatedTime(time);
        if (sceneType.getId() != null){
            baseMapper.updateById(sceneType);
        }else{
            sceneType.setCreatedBy(userid);
            sceneType.setCreatedTime(time);
            sceneType.setIsDelete(0);
            baseMapper.insert(sceneType);
        }
    }

    /**
     * 通过识别编号查询识别类型信息
     * @param cvId 识别编号
     * @return
     */
    @Override
    public DbAiSceneType querySceneTypeByCvId(Integer cvId){
        QueryWrapper<DbAiSceneType> wrapper = new QueryWrapper<>();
        wrapper.eq("is_delete",0);
        wrapper.eq("cv_id",cvId);
        DbAiSceneType sceneType = baseMapper.selectOne(wrapper);
        return sceneType;
    }

}
